/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itttq.gshop.domain.po.UserScorePo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.pbpm.service.UserScoreService;

@RestController
@RequestMapping("score")
public class UserScoreController extends GenericController {
	private static final long serialVersionUID = -9226967337965488L;
	public static final String CACHE_KEY = "userScore";
	@Autowired
	private UserScoreService userScoreService;
	
	/**
	 * 根据条件获取UserScorePo对象，并返回查询的数据总数
	 * 
	 * @param userScore
	 * @Author 迷蝶
	 * @return ResultPageTo<UserScorePo>
	 */
	@RequestMapping("list")
	public ResultPageTo<UserScorePo> getUserScores(@RequestBody UserScorePo userScore) {
		return userScoreService.getUserScores(userScore);
	}
	
	/**
	 * 根据条件获取UserScorePo对象，不返回查询的数据总数
	 * 
	 * @param userScore
	 * @Author 迷蝶
	 * @return ResultPageTo<UserScorePo>
	 */
	@RequestMapping("scores")
    public List<UserScorePo> getScores(@RequestBody UserScorePo userScore) {
        return userScoreService.getScores(userScore);
    }
	
	/**
	 * 根据ID获取UserScorePo对象，并放入redis缓存
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return UserScorePo
	 */
	@RequestMapping("/{id:^[0-9]*$}")
	@Cacheable(value=CACHE_KEY, key="'userScore_'+#id")
	public UserScorePo getUserScore(@PathVariable("id") int id) {
		return userScoreService.getUserScore(id);
	}
	
	/**
	 * 保存UserScorePo对象，并清除此ID的缓存。<br>
	 * 如果ID存在，则更新对象；ID不存在则插入对象。
	 * 
	 * @param userScore
	 * @Author 迷蝶
	 * @return UserScorePo
	 */
	@RequestMapping("/save")
	@CacheEvict(value=CACHE_KEY, key="'userScore_'+#userScore.id")
	public Integer saveUserScore(@RequestBody UserScorePo userScore) {
		int rs = 0;
		if(null == userScore) {return rs;}
		if(null == userScore.getId()) {
			rs = userScoreService.addUserScore(userScore);
		} else {
			rs = userScoreService.updateUserScore(userScore);
		}
		return rs;
	}
	
	/**
	 * 根据ID删除UserScorePo对象，并清除此ID的缓存。<br>
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return UserScorePo
	 */
	@RequestMapping("/del/{id:^[0-9]*$}")
	@CacheEvict(value=CACHE_KEY, key="'userScore_'+#id")
	public Integer delUserScore(@PathVariable("id") int id) {
		return userScoreService.delUserScore(id);
	}
}